package com.example.qts.fragments;


import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.qts.R;
import com.example.qts.SQL.MySql;
import com.example.qts.adapter.QSLadapter;
import com.example.qts.bean.User;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {


    private Button mQuey;
    private RecyclerView mHomeRe;
    private MySql mySql;
    private ArrayList<User> users;
    private QSLadapter qsLadapter;
    private int pos;
    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        initViews(view);
        registerForContextMenu(mHomeRe);
        return view;
    }

    private void initViews(View view) {
        mQuey = view.findViewById(R.id.quey);
        mHomeRe = view.findViewById(R.id.home_re);
        mHomeRe.setLayoutManager(new LinearLayoutManager(getContext()));
        mHomeRe.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayout.VERTICAL));
        users = new ArrayList<>();
        qsLadapter = new QSLadapter(getContext(), users);
        mHomeRe.setAdapter(qsLadapter);
        mySql = new MySql(getContext());
        for (int i = 0; i < 20; i++) {
            mySql.insertTeacher("adsdf"+i,15+1,"a",i);
        }
        mQuey.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<User> users1 = mySql.btQueryTeacher();
                users.addAll(users1);
                qsLadapter.notifyDataSetChanged();
            }
        });
        qsLadapter.setOnContextClickListener(new QSLadapter.OnContextClickListener() {
            @Override
            public void oncontextclicklistener(int position) {
                pos=position;
            }
        });
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0,0,0,"删除");
        menu.add(1,1,1,"修改");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case 0:
                mySql.deleteTeacher(pos);
                break;
            case 1:
                break;
        }
        return super.onContextItemSelected(item);

    }
}
